<?php
/**
 * Description of massmail
 *
 * @author Alex
 */
class massemail {

    var $champOptions = "", $messageStart, $messageEnd;
    var $message = "";


    function massemail()
    {
        if(isset($_POST['sendEmails']))
            $this->sendMassEmails();

        global $db, $lang;
        $q = 'SELECT c_id, c_title, count(*) as driver_count
              FROM ' . CHAMPIONSHIPS_TABLE . ', ' . CHAMPSIGNUPS_TABLE . '
              WHERE c_id = cs_champ
              GROUP BY c_id
              ORDER BY c_lastRace DESC';
        
        $result = $db->sql_query($q);
        while($row = $db->sql_fetchrow($result)) {
            $this->champOptions .= "<option value='{$row['c_id']}'>
                {$row['c_title']} ({$row['driver_count']} {$lang['TeamDrivers']})
                </option>";
        }
    }

    function render()
    {
        if($this->message != "")
            $this->message->render();

        global $lang, $skin_dir;
        $champOptions = $this->champOptions;
        $message_intro = $lang['EmailIntroduction'];
        $message_footer = $lang['EmailFooter'] . '<br/><br/>' . $lang['EmailNotification'];

        include $skin_dir . 'admin/massemail.tpl';
    }

    function sendMassEmails()
    {
        global $lang, $db;
        include_once 'includes/addMessage.php';
        if(! $this->validateParams())
            $this->message = new addMessage($lang['ErrorEmailDetails'],$lang['ErrorTitle']);
        else
        {
            $user_type = $_POST['user_type'];
            $title = $_POST['title'];

            $message = formatEmail($_POST['message']);

            
            if(isset($_POST['respectPrefs']))
                $massmailcheck = " AND u.user_allow_massemail = 1 ";
            else
                $massmailcheck = "";

            $users = Array();

            if($user_type == 2)
            {
                $user = Array();
                $user['drivername'] = $_POST['singleEmailAddress'];
                $user['emailaddress'] = $_POST['singleEmailAddress'];
                $users[] = $user;
            }
            else if($user_type == 1)
            {
                $champ_id = $_POST['champParam'];
                $q = "Select user_email as emailaddress, pf_drivername as drivername
                      From ". USERS_TABLE . " u, " . DRIVERNAME_TABLE . " d, " . USERSGROUPS_TABLE . " g, " . CHAMPSIGNUPS_TABLE . "
		      Where u.user_id = d.user_id AND u.user_id = g.user_id AND g.group_id = 3 AND
                            cs_champ = {$champ_id} AND cs_driver = u.user_id {$massmailcheck}";
                $result = $db->sql_query($q);
                while($row = $db->sql_fetchrow($result))
                {
                    $users[] = $row;
                }
            }
            else if($user_type == 0)
            {
                $q = "Select user_email as emailaddress, pf_drivername as drivername
			  From ". USERS_TABLE . " u, " . DRIVERNAME_TABLE . " d, " . USERSGROUPS_TABLE . " g
			  Where u.user_id = d.user_id AND u.user_id = g.user_id AND g.group_id = 3 {$massmailcheck}";
                $result = $db->sql_query($q);
                while($row = $db->sql_fetchrow($result))
                {
                    $users[] = $row;
                }
            }
            
            include_once('includes/email.php');
            $x = 0;
            foreach($users as $user)
            {
		$email = new email('admin_massemail', $user);
		$email->addReplacement('{DRIVERNAME}' , 'drivername', 1);
		$email->addReplacement('{EMAILADRESS}' , 'emailaddress', 1);
		$email->addReplacement('{TITLE}' , $title );
		$email->addReplacement('{MESSAGE}' , $message );

                $email->sendEmail();
                $x++;
            }

            $this->message = new addMessage($lang['AdminMassEmailSuccessTitle'],
                        sprintf($lang['AdminMassEmailSuccessMessage'],$x));
        }
    }

    function validateParams()
    {
        $valid = isset($_POST['user_type']) && is_numeric($_POST['user_type']) &&
                 $_POST['user_type'] >= 0 && $_POST['user_type'] < 3 &&
                 isset($_POST['title']) && strlen($_POST['title']) > 5 &&
                 isset($_POST['message']) && strlen($_POST['message']) > 50;
        if($valid && $_POST['user_type'] == 1)
            $valid = $valid && isset($_POST['champParam']) && is_numeric($_POST['champParam']);
        else if($valid && $_POST['user_type'] == 2)
            $valid = $valid && isset($_POST['singleEmailAddress']) &&
                check_email_address($_POST['singleEmailAddress']);

        return $valid;
    }
}
?>
